package com.coyote.web.controller;


import com.coyote.common.base.IdListParam;
import com.coyote.common.base.PageParam;
import com.coyote.common.base.PageUtils;
import com.coyote.common.base.Responses;
import com.coyote.entity.HelloPay;
import com.coyote.web.service.ExampleTwoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

import static com.coyote.common.Apis.EXAMPLE_TWO;
import static com.coyote.common.Apis.OPENAPI;

@Tag(name = "API文档示例", description = "基础操作的API")
@RestController
@RequestMapping({EXAMPLE_TWO, OPENAPI + EXAMPLE_TWO})
public class ExampleTwoController {

    @Resource
    private ExampleTwoService exampleTwoService;

    @Operation(summary = "新增（单条）", description = "添加数据")
    @PostMapping("/insert")
    public Responses<Boolean> insert(@RequestBody HelloPay param) {
        param.setId(null);
        boolean result = exampleTwoService.insertt(param);
        return Responses.ok(result);
    }

    @Operation(summary = "删除（单条）", description = "根绝id删除数据")
    @DeleteMapping("/delete/{id}")
    public Responses<Boolean> deleteById(@PathVariable("id") Serializable id) {
        boolean result = exampleTwoService.removeById(id);
        return Responses.ok(result);
    }

    @Operation(summary = "删除（批量）", description = "根绝ids批量删除数据")
    @DeleteMapping("/delete")
    public Responses<Boolean> deleteById(@Valid @RequestBody IdListParam param) {
        boolean result = exampleTwoService.removeByIds(param.getIds());
        return Responses.ok(result);
    }

    @Operation(summary = "修改", description = "根绝id修改数据")
    @PutMapping("/update")
    public Responses<Boolean> updateById(@Valid @RequestBody HelloPay param) {
        // 若使用了乐关锁插件，这里就不能修改成功，因为会自动在条件中拼version条件，所以需要先查询version
        // 或使用updateOptimisticLock方法
        if (ObjectUtils.isEmpty(param.getId())) {
            throw new RuntimeException("id不能为空");
        }
        boolean result = exampleTwoService.updateById(param);
        return Responses.ok(result);
    }

    @Operation(summary = "修改（乐观锁）", description = "根据id修改数据")
    @PutMapping("/updateOptimisticLock")
    public Responses<Boolean> updateOptimisticLock(@Valid @RequestBody HelloPay param) {
        // 数据库实时查询version字段值，设置version
        HelloPay bo = exampleTwoService.getById(param.getId());
        Integer version = bo.getRevision();
        param.setRevision(version);
        // 执行修改
        boolean flag = exampleTwoService.updateById(param);
        return Responses.ok(flag);
    }

    @Operation(summary = "查询（详情）", description = "根据id查看详情")
    @GetMapping("/info/{id}")
    public Responses<HelloPay> getInfo(@PathVariable("id") Serializable id) {
        HelloPay bo = exampleTwoService.getById(id);
        return Responses.ok(bo);
    }


    @Operation(summary = "所有列表", description = "查询所有数据")
    @GetMapping("/listAll")
    public Responses<List<HelloPay>> listAll() {
        List<HelloPay> boList = exampleTwoService.list();
        return Responses.ok(boList);
    }


    @Operation(summary = "分页列表", description = "分页列表展示(字段eq匹配,默认创建时间降序排序)")
    @PostMapping({"/listPage", "/page"})
    public Responses<PageUtils<HelloPay>> listPage(@RequestBody PageParam pageParam) {
        PageUtils<HelloPay> pageBo = exampleTwoService.listPage(pageParam.getPage(), pageParam.getPageSize());
        return Responses.ok(pageBo);
    }


    @Operation(summary = "分页列表", description = "分页列表展示(字段eq匹配,默认创建时间降序排序)")
    @PostMapping("/listWrapper")
    public Responses<PageUtils<HelloPay>> listWrapper(@RequestBody HelloPay param) {
        PageUtils<HelloPay> pageBo = exampleTwoService.listPage(param);
        return Responses.ok(pageBo);
    }


}
